package com.have.blog.admin.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.SystemParamDto;
import com.have.blog.admin.redis.AdminSystemParamCacheService;
import com.have.blog.admin.service.SystemParamService;
import com.have.blog.base.constant.ErrorCode;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.exception.QueryException;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.SysParamsMapper;
import com.have.blog.model.models.SysParams;
import com.have.blog.model.models.SysParamsExample;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings("all")
public class SystemParamServiceImpl implements SystemParamService {
    @Autowired
    private SysParamsMapper sysParamsMapper;
    @Autowired
    private AdminSystemParamCacheService systemParamCacheService;

    /**
     * 通过关键词获取系统参数的值
     * @param paramKey
     * @return
     */
    @Override
    public String getSystemParamValueByKey(String paramKey) {

        SysParams sysParams = systemParamCacheService.getSystemParamByParamKey(paramKey);
        if (sysParams != null) {
            return sysParams.getParamsValue();
        }
        SysParamsExample example = new SysParamsExample();
        example.createCriteria()
                .andParamsKeyEqualTo(paramKey);
        List<SysParams> SysParams = sysParamsMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(SysParams)) {
            throw new QueryException(ErrorCode.PLEASE_CONFIGURE_SYSTEM_PARAMS, AdminMessageConstant.PLEASE_CONFIGURE_SYSTEM_PARAMS);
        }
        sysParams = SysParams.get(0);
        systemParamCacheService.saveSystemParamByParamKey(sysParams, paramKey, 1, TimeUnit.DAYS);
        return sysParams.getParamsValue();
    }

    @Override
    public Wrapper<CommonPage<SystemParamDto>> getPageList(SystemParamDto systemParamDto) {
        Page page = PageHelper.startPage(systemParamDto.getPageNum(), systemParamDto.getPageSize());
        SysParamsExample example = new SysParamsExample();
        SysParamsExample.Criteria criteria = example.createCriteria()
                                                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (!StringUtils.isEmpty(systemParamDto.getParamsName()) && !StringUtils.isEmpty(systemParamDto.getParamsName().trim())){
            criteria.andParamsNameLike("%" + systemParamDto.getParamsName().trim() + "%");
        }
        if (!StringUtils.isEmpty(systemParamDto.getParamsKey()) && !StringUtils.isEmpty(systemParamDto.getParamsKey().trim())) {
            criteria.andParamsKeyLike("%" + systemParamDto.getParamsKey() + "%");
        }

        example.setOrderByClause("create_time desc");
        List<SysParams> sysParamList = sysParamsMapper.selectByExample(example);
        List<SystemParamDto> resultList = MyBeanUtils.copyListProperties(sysParamList, SystemParamDto::new);
        CommonPage<SystemParamDto> commonPage = CommonPage.restPage(resultList);
        commonPage.setPageSize(page.getPageSize());
        commonPage.setTotal(page.getTotal());
        commonPage.setPageNum(page.getPageNum());
        return Wrapper.success(commonPage);
    }

    @Override
    public Wrapper addSysParams(SystemParamDto systemParamDto) {
        SysParamsExample example = new SysParamsExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andParamsKeyEqualTo(systemParamDto.getParamsKey());
        List<SysParams> SysParams = sysParamsMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(SysParams)) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
        }
        SysParams sysParams = new SysParams();
        MyBeanUtils.copyProperties(systemParamDto, sysParams);
        sysParams.setStatus(BlogStatus.ENABLE.getStatus());
        int result = sysParamsMapper.insert(sysParams);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Override
    public Wrapper editSysParams(SystemParamDto systemParamDto) {
        SysParams sysParams = sysParamsMapper.selectByPrimaryKey(systemParamDto.getUid());
        if (sysParams == null) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_NOT_EXIST);
        }
        sysParams.setParamsKey(systemParamDto.getParamsKey());
        sysParams.setParamsName(systemParamDto.getParamsName());
        sysParams.setParamsValue(systemParamDto.getParamsValue());
        sysParams.setParamsType(systemParamDto.getParamsType());
        sysParams.setRemark(systemParamDto.getRemark());
        sysParams.setSort(systemParamDto.getSort());
        sysParams.setUpdateTime(new Date());
        int result = sysParamsMapper.updateByPrimaryKeySelective(sysParams);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteBatchSysParams(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        SysParamsExample example = new SysParamsExample();
        example.createCriteria()
                .andUidIn(uids);
        SysParams sysParams = new SysParams();
        sysParams.setStatus(BlogStatus.DISABLE.getStatus());
        int result = sysParamsMapper.updateByExampleSelective(sysParams, example);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }
}
